﻿using BO.News;
using BugManager;
using GUI.Controls.Embed;
using GUI.Extensions;
using System;
using System.Linq;

namespace GUI.Controls.News
{
    public partial class EmbedZones : System.Web.UI.UserControl
    {

        private const string Template1 = "/Controls/News/EmbedGroup1.ascx";
        private const string Template2 = "/Controls/News/EmbedGroup2.ascx";
        private const string Template3 = "/Controls/News/EmbedGroup3.ascx";
        private const string Template4 = "/Controls/News/EmbedGroup4.ascx";
        private const string Template5 = "/Controls/Embed/EmbedTemplate5.ascx";
        private const string Template6 = "/Controls/Embed/EmbedTemplate6.ascx";

        protected void Page_Load(object sender, EventArgs e)
        {
            var bo = CacheSettingExtensions.EnableCacheHtml
                         ? NewsBO_NoCache.GetInstance()
                         : NewsBO.GetInstance();
            var embedGroup = bo.GetEmbedGroup();

            if (embedGroup != null && embedGroup.Count > 0)
            {
                foreach (var group in embedGroup)
                {
                    if (group != null && group.Visible)
                    {
                        //group.TepmlateId = 4;
                        switch (group.TepmlateId)
                        {
                            case 1:
                                try
                                {
                                    var control = (EmbedGroup1)LoadControl(Template1);
                                    Controls.Add(control);
                                    if (group.Zones != null)
                                        control.DataBind(group.Zones.Length > 0 ? group.Zones[0].ZoneId : 0, group.Zones.Length > 1 ? group.Zones[1].ZoneId : 0);
                                }
                                catch (Exception ex)
                                {
                                    ErrorManager.Log(ex.ToString());
                                }
                                break;

                            case 2:
                                try
                                {
                                    if (group.Zones != null)
                                    {
                                        //template dành cho 1 chuyên mục
                                        if (group.Zones.Length == 1)
                                        {
                                            var control = (EmbedTemplate6)LoadControl(Template6);
                                            if (control != null)
                                            {
                                                Controls.Add(control);
                                                control.DataBind(group.Zones[0].ZoneId);
                                            }
                                            
                                        }
                                        else
                                        {


                                            var control = (EmbedGroup2)LoadControl(Template2);

                                            Controls.Add(control);
                                            control.DataBind(group.Zones.Length > 0 ? group.Zones[0].ZoneId : 0, group.Zones.Length > 1 ? group.Zones[1].ZoneId : 0);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    ErrorManager.Log(ex.ToString());
                                }
                                break;

                            case 3:
                                try
                                {
                                    var control = (EmbedGroup3)LoadControl(Template3);
                                    if (control != null)
                                    {
                                        Controls.Add(control);
                                        control.DataBind(@group.Zones != null && @group.Zones.Length > 0 ? group.Zones[0].ZoneId : 0, @group.Zones != null && @group.Zones.Length > 1 ? group.Zones[1].ZoneId : 0);

                                    }
                                }
                                catch (Exception ex)
                                {
                                    ErrorManager.Log(ex.ToString());
                                }
                                break;

                            case 4:
                                try
                                {
                                   
                                    var control = (EmbedGroup4)LoadControl(Template4);
                                    if (control != null)
                                    {
                                        Controls.Add(control);
                                        control.DataBind(@group.Zones != null && @group.Zones.Length > 0 ? group.Zones[0].ZoneId : 0, @group.Zones != null && @group.Zones.Length > 1 ? group.Zones[1].ZoneId : 0);

                                    }
                                }
                                catch (Exception ex)
                                {
                                    ErrorManager.Log(ex.ToString());
                                }
                                break;
                            case 5:
                                try
                                {
                                    var control = (EmbedTemplate5)LoadControl(Template5);
                                    if (control != null)
                                    {
                                        if (@group.Zones != null)
                                        {
                                            var zoneid = @group.Zones[0].ZoneId;
                                            if (zoneid > 0)
                                            {
                                                var zone = NewsBO.GetInstance().GetCategoryDetail(zoneid);
                                                if (zone != null)
                                                {
                                                    var listNews = bo.ImportNewsWhileNotEnough(zoneid, 5, 0);
                                                    if (listNews != null && listNews.Count > 0)
                                                    {
                                                        Controls.Add(control);
                                                        var topNews = listNews.Take(1);
                                                        topNews.FirstOrDefault().RelateNews = listNews.Skip(1).Take(2).ToArray();
                                                        control.DataBind(
                                                            zone,
                                                            topNews.ToArray(),
                                                            listNews.Skip(3).Take(2).ToArray());
                                                    }
                                                }
                                            }
                                        }
                                        
                                    }
                                }
                                catch (Exception ex)
                                {
                                    ErrorManager.Log(ex.ToString());
                                }
                                break;
                        }
                    }
                }
                return;
            }
            Visible = false;
        }
    }
}